package com.dylon.seapiweb.controller;

import com.dylon.common.base.AjaxResult;
import com.dylon.common.page.TableDataInfo;
import com.dylon.system.domain.SysProject;
import com.dylon.system.service.ISysProjectService;
import lombok.extern.log4j.Log4j2;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 项目信息
 */
@Controller
@RequestMapping("/system/project")
@Log4j2
public class SysProjectController extends BaseController{
    private String prefix = "system/project";

    @Autowired
    private ISysProjectService projectService;

    @RequiresPermissions("system:project:view")
    @GetMapping()
    public String project()
    {
        return prefix + "/project";
    }

    @RequiresPermissions("system:project:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SysProject project){
        startPage();
        List<SysProject> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 新增项目
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存模块
     */
    @RequiresPermissions("system:project:add")
    @PostMapping("/add")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult addSave(SysProject project){
        project.setCreateBy(getSysUser().getUserName());
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目
     */
    @GetMapping("/edit/{projectId}")
    public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap){
        mmap.put("project", projectService.selectProjectById(projectId));
        return prefix + "/edit";
    }

    /**
     * 修改保存项目
     */
    @RequiresPermissions("system:project:edit")
    @PostMapping("/edit")
    @Transactional(rollbackFor = Exception.class)
    @ResponseBody
    public AjaxResult editSave(SysProject project){
        project.setUpdateBy(getSysUser().getUserName());
        return toAjax(projectService.updateProject(project));
    }

    @RequiresPermissions("system:project:remove")
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids){
        try{
            return toAjax(projectService.deleteProjectByIds(ids));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }

    /**
     * 校验模块名称
     */
    @PostMapping("/checkProjectNameUnique")
    @ResponseBody
    public String checkProjectNameUnique(SysProject project){
        return projectService.checkProjectNameUnique(project);
    }

    /**
     * 模块状态修改
     */
    @RequiresPermissions("system:project:edit")
    @PostMapping("/changeStatus")
    @ResponseBody
    public AjaxResult changeStatus(SysProject project)
    {
        return toAjax(projectService.changeStatus(project));
    }

    /**
     * 删除
     */
    @RequiresPermissions("system:project:remove")
    @PostMapping("/remove/{projectId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("projectId") Long projectId){
        try{
            return toAjax(projectService.deleteProjectById(projectId));
        }catch (Exception e){
            return error(e.getMessage());
        }
    }
}
